import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {List, Detail, Brand, Dealer, PrevList, PackingList, Payment, RebateFee, LockedFee, HistoryList, HistoryDetail} from './DTO';
const baseUrl = '/parts/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '备件备件箱单发票'
});
const getInitDataDesc = formatMessage({
    id: 'api.message.getInitDataDesc',
    defaultMessage: '初始化{resourceName}'
}, {resourceName});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const uploadSAPDesc = formatMessage({
    id: 'api.message.uploadSAPDesc',
    defaultMessage: '上传SAP'
});

const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});

const dealerDesc = formatMessage({
    id: 'api.message.dealerDesc',
    defaultMessage: '查询总代'
});

const paymentTypeDesc = formatMessage({
    id: 'api.message.paymentTypeDesc',
    defaultMessage: '查询付款方式'
});


const rebateFeeDesc = formatMessage({
    id: 'api.message.rebateFeeDesc',
    defaultMessage: '查询客户返利余额'
});
const lockedFeeDesc = formatMessage({
    id: 'api.message.lockedFeeDesc',
    defaultMessage: '查询锁定金额'
});
const packingListDesc = formatMessage({
    id: 'api.message.packingListDesc',
    defaultMessage: '查询备件装箱单'
});

const getHistoryListDesc = formatMessage({
    id: 'api.message.getHistoryListDesc',
    defaultMessage: '查询备件预箱单发票历史列表'
});

const getHistoryDetailDesc = formatMessage({
    id: 'api.message.getHistoryDetailDesc',
    defaultMessage: '查询备件预箱单发票历史详情'
});

const getPartClaimsDesc = formatMessage({
    id: 'api.message.getPartClaimsDesc',
    defaultMessage: '导出箱单发票备件清单'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/partInvoiceManage/init`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDataDesc));

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partInvoices/forManage?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/partInvoices/${id}/forManage`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 新增
export const addInfo = data => request(`${baseUrl}/partInvoices/forManage`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));


// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/partInvoices/${id}/forManage`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 作废
export const abandonInfo = (id, data) => request(`${baseUrl}/partInvoices/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

// 上传SAP
export const uploadSAPInfo = (id, data) => request(`${baseUrl}/partInvoices/${id}/uploadSAP`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, uploadSAPDesc));

// 查询前一张非作废状态的备件箱单发票信息
export const getPrevList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partInvoices/forManageAdd?${queryString}`, null, PrevList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

import {baseDataStatus} from '../Enum';
// 获取品牌
export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brand)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, brandDesc));
};

    
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forPartInvoiceManage?${queryString}`, null, Dealer)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, dealerDesc));
};

//根据条件查询总代子公司
export const getDealersSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/dealers/forPartInvoiceManage/simple?${queryString}`, null, Dealer)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, dealerDesc))
        .then(res => {
            if(res.ok) return res.data.content;
            return false;
        });
};


// 查询备件装箱单
export const getPackingLists = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/packingLists/forPartInvoiceManage?${queryString}`, null, PackingList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, packingListDesc));
};

// 查询付款类型
export const getPaymentType = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partSaleOrders/forPartInvoiceManage?${queryString}`, null, Payment)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, paymentTypeDesc));
};

// 查询客户资金账户返利余额
export const getRebateFee = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/rebateFees/forPartInvoiceManage?${queryString}`, null, RebateFee)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, rebateFeeDesc));
};

// 查询锁定金额
export const getLockedFee = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partInvoices/forLockedFee?${queryString}`, null, LockedFee)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, lockedFeeDesc));
};

// 导出
export const exportInfo = queryCondition => request(`${baseUrl}/partInvoiceDetails/export`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(queryCondition)
})
    .then(handleFetchResultNotification(REQUEST_TYPE.submit))
    .then(res => res.data);

//查找备件预箱单发票历史列表
export const getHistoryList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partInvoices/forHistory/forCurrentUser?${queryString}`, null, HistoryList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getHistoryListDesc));
};

//查找备件预箱单发票历史详情
export const getHistoryDetail = id => request(`${baseUrl}/partInvoices/${id}/forHistory`, null, HistoryDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getHistoryDetailDesc));
//历史附件下载
export const getHistoryDownload = data => {
    const queryString = stringify(data);
    return request(`${baseUrl}/files/ftpIsDownload?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, null));
};
// 导出箱单发票备件清单
export const exportPartInvoices = id => request(`${baseUrl}/partInvoices/${id}/ExportPartClaimDetail`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, getPartClaimsDesc)).then(res => res.data);
